/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.trafficando.domain.traffic;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Domenico Maria Giffone
 */
public abstract class TrafficEventCategoryDataProvider {

    protected abstract void registerTypes() throws Exception;

    protected final void registerType(String term, String scheme, String label) {
        TrafficEventCategory.registerType(term, scheme, label);
    }

    static class DefaultProvider extends TrafficEventCategoryDataProvider {

        /** Regex format for the csv line. */
        private static final Pattern REGEX_LINE = Pattern.compile("([A-Z_]+),(.+),(.+)#?.*");

        @Override
        protected void registerTypes() throws Exception {
            InputStream in = getClass().getResourceAsStream("/com/appspot/trafficando/domain/traffic/TrafficEventCategoryData.csv");
            if (in == null) {
                throw new FileNotFoundException("Data file /com/appspot/trafficando/domain/traffic/TrafficEventcategoryData.csv not found");
            }

            BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String line;
            while ((line = reader.readLine()) != null) {
                Matcher matcher = REGEX_LINE.matcher(line);
                if (matcher.matches()) {
                    String term = matcher.group(1);
                    String scheme = matcher.group(2);
                    String label = matcher.group(3);
                    registerType(term, scheme, label);
                }
            }

        }
    }
}
